package com.chinasoft.controller;

import com.chinasoft.domain.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;

@Controller
@RequestMapping("/user")
public class UserController {

    // localhost:80/requestParam1?name=lilei&age=18
    @RequestMapping("/requestParam1")
    public String requestParam1(String name,int age){
        System.out.println(name + "," + age);
        return "page.jsp";
    }
    // localhost:80/requestParam2?userName=tom
    @RequestMapping("/requestParam2")
    public String requestParam2(@RequestParam(value = "userName",required = false,defaultValue = "unknow") String name){
        System.out.println(name);
        return "page.jsp";
    }

    // localhost:80/requestParam3?name=lilei&age=18
    @RequestMapping("/requestParam3")
    public String requestParam3(User user){
        System.out.println(user);
        return "page.jsp";
    }
    // localhost:80/requestParam4?name=lilei&age=18
    @RequestMapping("/requestParam4")
    public String requestParam4(User user,String age){
        System.out.println(user);
        System.out.println(age);
        return "page.jsp";
    }

    // localhost:80/user/requestParam5?address.city=beijing
    @RequestMapping("/requestParam5")
    public String requestParam5(User user){
        System.out.println(user.getAddress().getCity());

        return "page.jsp";
    }

    // localhost:80/user/requestParam6?nick=tom&nick=jack&nick=jerry
    @RequestMapping("/requestParam6")
    public String requestParam6(User user){
        System.out.println(user);

        return "page.jsp";
    }
    // localhost:80/user/requestParam7?addresses[0].city=shanghai&addresses[1].province=hebei
    @RequestMapping("/requestParam7")
    public String requestParam7(User user){
        System.out.println(user.getAddresses());
        return "page.jsp";
    }

    // localhost:80/user/requestParam8?addressMap['job'].city=beijing&addressMap['home'].province=jilin
    @RequestMapping("/requestParam8")
    public String requestParam8(User user){
        System.out.println(user.getAddressMap());
        return "page.jsp";
    }

    // localhost:80/user/requestParam9?nick=tom&nick=jack&nick=jerry
    @RequestMapping("/requestParam9")
    public String requestParam9(String[] nick){
        System.out.println(nick[0]);
        System.out.println(nick[1]);
        return "page.jsp";
    }

    // localhost:80/user/requestParam10?date=1999-09-09
    @RequestMapping(value = "/requestParam10",method = RequestMethod.GET)
    public String requestParam10(@DateTimeFormat(pattern = "yyyy-MM-dd") Date date){
        System.out.println(date);
        return "page.jsp";
    }

}